package com.wmh.baseservice.merchant.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wmh.baseservice.admin.pojo.AdInfo;
import com.wmh.baseservice.admin.pojo.AdMenu;
import com.wmh.baseservice.admin.pojo.AdRoleMenu;
import com.wmh.baseservice.admin.pojo.AdRolePermissions;
import com.wmh.baseservice.admin.tools.enums.AdMenuFlag;
import com.wmh.baseservice.common.annotation.parameterverify.ParameterVerify;
import com.wmh.baseservice.common.utils.CommUtil;
import com.wmh.baseservice.common.utils.returnbody.ResultCodeInfo;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.merchant.pojo.MerchantMenu;
import com.wmh.baseservice.merchant.pojo.MerchantRoleMenu;
import com.wmh.baseservice.merchant.pojo.MerchantRolePermissions;
import com.wmh.baseservice.merchant.service.MerchantRoleMenuService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import com.wmh.baseservice.merchant.service.MerchantRolePermissionsService;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wmh generator
 * @since 2021-12-24
 */
@Api(tags = "商家角色权限相关接口")
@RestController
@RequestMapping(value = "merchant/merchantRolePermissions", produces = "text/plain;charset=utf-8")
@RequiresRoles("merchant")
@RequiresPermissions("所有权限")
public class MerchantRolePermissionsController {

    @Resource
    private MerchantRolePermissionsService merchantRolePermissionsService;
    @Resource
    private MerchantRoleMenuService merchantRoleMenuService;

    @ApiOperation("列表(无分页)")
    @PostMapping("listNoPage")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "id", value = "id", required = false),
    })
    public String listNoPage(Long id) {
        JSONObject jsonObject = new JSONObject(true);
        List<MerchantRolePermissions> list = merchantRolePermissionsService.list(new QueryWrapper<MerchantRolePermissions>().eq(MerchantRolePermissions.TYPE, 2).isNull(MerchantRolePermissions.MERCHANT_ID));
        List<String> listCheck = merchantRolePermissionsService.list(new QueryWrapper<MerchantRolePermissions>().eq(MerchantRolePermissions.TYPE, 2).eq(MerchantRolePermissions.MERCHANT_ID, id)).stream().map(MerchantRolePermissions::getPermissions).collect(Collectors.toList());
        jsonObject.put("list", list);
        jsonObject.put("listCheck", listCheck);
        return ReturnBody.success(jsonObject);
    }

    @ApiOperation("获取该角色拥有的菜单权限")
    @ApiImplicitParam(paramType = "query", dataTypeClass = Integer.class, name = "id", value = "角色ID", required = true)
    @PostMapping("getRoleMenuId")
    public String getRoleMenuId(Long roleId) {
        if (!CommUtil.checkNull(roleId)) {
            return ReturnBody.error(ResultCodeInfo.PARAM_ERROR);
        }
        MerchantRoleMenu adminRoleMenu = new MerchantRoleMenu();
        adminRoleMenu.setRoleId(roleId);
        List<MerchantMenu> list = merchantRoleMenuService.selectRoleMenu(adminRoleMenu);
        List<Long> integers = new ArrayList<>();
        list.forEach(item -> integers.add(item.getId()));
        return ReturnBody.success(integers);
    }

    @ApiOperation("分配菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Integer.class, name = "roleId", value = "角色ID", required = true),
            @ApiImplicitParam(paramType = "query", dataTypeClass = Integer[].class, name = "menuIds", value = "菜单ID数组", required = true)
    })
    @PostMapping("setRoleMenu")
    @ParameterVerify(notNull = {"roleId"})
    public String setRoleMenu(Long roleId, Long[] menuIds) {
        if (menuIds == null) {
            return ReturnBody.error(ResultCodeInfo.PARAM_ERROR);
        }
        if (menuIds.length < 1) {
            return ReturnBody.error("请选择菜单");
        }
        return merchantRoleMenuService.setRoleMenu(roleId, menuIds);
    }

    @ApiOperation("分配权限")
    @PostMapping("setPer")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "id", value = "id", required = false),
    })
    public String setPer(Long id, String rolePerId) {
        List<MerchantRolePermissions> merchantRolePermissionsList = new ArrayList<>();
        List<String> rolePerLongId = JSON.parseArray(rolePerId, String.class);
        rolePerLongId.forEach(item -> {
            MerchantRolePermissions merchantRolePermissions = new MerchantRolePermissions();
            merchantRolePermissions.setMerchantId(id).setPermissions(item).setType(2);
            merchantRolePermissionsList.add(merchantRolePermissions);
        });
        merchantRolePermissionsService.setPer(id, merchantRolePermissionsList);
        return ReturnBody.success();
    }
}
